package cn.hwadee.muying.controller.sys;


import cn.hwadee.muying.commons.base.BaseController;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import cn.hwadee.muying.entity.Organization;
import cn.hwadee.muying.service.OrganizationService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mankind
 * @since 2018-04-20
 */
@Controller
@RequestMapping("/sys/org")
public class OrgController extends BaseController {
    @Autowired
    private OrganizationService orgService;

    @ModelAttribute("org")
    public Organization get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return orgService.selectById(id);
        } else {
            return new Organization();
        }
    }

    @RequiresPermissions("sys:org:list")
    @RequestMapping(value = "/list", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/sys/org/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:org:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public GridResult list(String keyword) throws Exception{
        MyPage<Organization> page = getPage();
        page = orgService.list(page,getCurrentUser(),keyword);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    @RequiresPermissions(value={"sys:org:edit","sys:org:add"}, logical = Logical.OR)
    @RequestMapping(value = "/edit", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String edit(@ModelAttribute("org") Organization org, Model model) throws Exception{
        model.addAttribute("org",org);
        return "/sys/org/edit";
    }

    @RequiresPermissions(value = {"sys:org:edit","sys:org:perm"}, logical = Logical.OR)
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute("org") Organization org){
        Map<String,Object> result = new HashedMap();
        try {
            if(org.getId() != null && org.getId() > 0){
                orgService.updateById(org);
            }else {
                orgService.insert(org);
            }
            result.put("status",200);
            result.put("msg","机构信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","机构信息保存失败");
        }
        return result;
    }

    /**
     * 删除用户
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:org:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashedMap();
        try {
            if(id != null && id > 0){
                orgService.deleteById(id);
                //TODO 删除机构下所有角色和用户
                result.put("status",200);
                result.put("msg","删除机构成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的机构");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除机构失败");
        }
        return result;
    }

}

